﻿using MediatR;
using ThreeTeams.API.Application.Command;
using ThreeTeams.API.Application.Command.ShelvesCommand;
using ThreeTeams.ErrorCode;
using ThreeTeams.Domain;
using ThreeTeams.Infrastructure;
using System.Threading;
using System.Threading.Tasks;

namespace ThreeTeams.API.Application.CommandHandler.ProductCommandHandler
{
    /// <summary>
    /// 货架添加命令处理器
    /// </summary>
    public class AddShelvesCommandHandler : IRequestHandler<AddShelvesCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Shelves> baseRepository;

        public AddShelvesCommandHandler(IBaseRepository<Shelves> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public async Task<ApiResult<int>> Handle(AddShelvesCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var shelves = new Shelves
            {
                Name = request.Name,
                Code = request.Code,
                BoxCount = request.BoxCount,
                SkuTypeCount = request.SkuTypeCount,
                SkuTotalCount = request.SkuTotalCount,
                Remark = request.Remark
            };
            await baseRepository.Create(shelves);
            res.Code = ApiEnum.Ok;
            res.Message = "添加成功";
            res.Data = shelves.Id;
            return res;
        }
    }
}